2606. Minimum and maximum

 

Find the minimum and maximum of two positive integers.

 

Input. Two positive integers a and b (a, b ≤ 109).

 

Output. Print the minimum followed by the maximum of the two numbers a and b in one line.

 

Sample input 1

Sample output 1

4 2

2 4

 

 

Sample input 2

Sample output 2

10 100

10 100

 

 

SOLUTION

conditional statement

 

Algorithm analysis

Compare two numbers and first print the smaller one, followed by the larger one.

The second solution involves checking if a > b. If true, swap their values using a third variable. Then, print the numbers a and b.

 

Algorithm implementation

Read the input data.

 

scanf("%d %d",&a,&b);

 

Compare and print the numbers in the required order.

 

if (a < b)

  printf("%d %d\n",a,b);

else

  printf("%d %d\n",b,a);

 

Algorithm implementation – swap

Read the input data.

 

scanf("%d %d",&a,&b);

 

If a > b, swap the values of a and b.

 

if (a > b)

{

  temp = a; a = b; b = temp;

}

 

Print the values a and b, where a b.

 

printf("%d %d\n",a,b);

 

Algorithm implementation ternary operator

Read the input data.

 

scanf("%d %d",&a,&b);

 

Compute the minimum min and maximum max values between the two numbers a and b.

 

min = (a < b) ? a : b;

max = (a > b) ? a : b;

 

Print the minimum min and maximum max of the two numbers a and b.

 

printf("%d %d\n",min,max);

 

Algorithm implementation using the functions

 

#include <stdio.h>

 

int a, b;

 

int min(int a, int b)

{

  return (a < b) ? a : b;

}

 

int max(int a, int b)

{

  return (a > b) ? a : b;

}

 

int main(void)

{

  scanf("%d %d",&a,&b);

  printf("%d %d\n",min(a,b),max(a,b));

  return 0;

}

 

Java implementation

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int a = con.nextInt();

    int b = con.nextInt();

   

    System.out.println(Math.min(a,b) + " " + Math.max(a,b));

    con.close();

  }

}  

 

Python implementation

Read the input data.

 

a, b = map(int, input().split())

 

If a > b, swap the values of a and b.

 

if a > b:

  a, b = b, a

 

Print the values a and b, where a b.

 

print(a, b)

 

C# realization

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace ConsoleAppCSharp

{

  class Program

  {

    static void Main(string[] args)

    {

      string[] values = Console.ReadLine().Split(' ');

      int x = int.Parse(values[0]);

      int y = int.Parse(values[1]);

 

      int min = (x < y) ? x : y;

      int max = (x > y) ? x : y;

      Console.WriteLine("{0} {1}", min, max);

    }

  }

}